package com.dominator.utils.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.dominator.utils.system.PropertiesLoader;

public class JdbcService {

	private static String driver = "com.mysql.jdbc.Driver";
	private static Connection conn = null;
	private static Statement stmt = null;
	private static ResultSet rs = null;
	public static ConnectionPool connectionPool = null;
    public static PropertiesLoader propertiesLoader=new PropertiesLoader("sysconfig.properties");
    public static String url=propertiesLoader.getProperty("jdbc.url");
    public static String userName=propertiesLoader.getProperty("jdbc.username");
    public static String password=propertiesLoader.getProperty("jdbc.password");

	public JdbcService() throws Exception {
		if (connectionPool == null) {
			if(url.matches("(.*)log4jdbc:(.*)")){
				url=url.replace("log4jdbc:", "");
			}
			connectionPool = new ConnectionPool(driver, url, userName, password);
			connectionPool.createPool();
		}
	}
	
	public static String info(){
		JSONObject info=new JSONObject();
		info.put("url", url);
		info.put("userName", userName);
		info.put("password", password);
		return info.toJSONString();
	}
	
	

	public static void insert(String sql)  {
		try {
			conn = connectionPool.getFreeConnection();
			stmt = conn.createStatement();
			stmt.execute(sql);
			stmt.close();
			connectionPool.returnConnection(conn);
		} catch (Exception e) {
			return;
		}
		return;
	}

	public static String select(String sql)  {
		String result = null;
		try {
			result = null;
			conn = connectionPool.getFreeConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				result = rs.getString(1);
			}
			rs.close();
			stmt.close();
			connectionPool.returnConnection(conn);
		} catch (Exception e) {
			return e.getMessage();
		}
		return result ;
	}

	public static String update(String sql) {
		try {
			conn = connectionPool.getFreeConnection();
			stmt = conn.createStatement();
			stmt.execute(sql);
			stmt.close();
			connectionPool.returnConnection(conn);
		} catch (Exception e) {
			return e.getMessage();
		}
		return "ok";
	}

	public static String delete(String sql) throws Exception {
		try {
			conn = connectionPool.getFreeConnection();
			stmt = conn.createStatement();
			stmt.execute(sql);
			stmt.close();
			connectionPool.returnConnection(conn);
		} catch (Exception e) {
			return e.getMessage();
		}
		return "ok";
	}
	
	
	public static String selectAll(String sql) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		JSONObject data=new JSONObject();
		try {
			Class.forName(driver);
			conn = connectionPool.getFreeConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			ResultSetMetaData md = rs.getMetaData(); // 获得结果集结构信息,元数据
			int columnCount = md.getColumnCount(); // 获得列数
			while (rs.next()) {
				Map<String, Object> rowData = new HashMap<String, Object>();
				for (int i = 1; i <= columnCount; i++) {
					rowData.put(md.getColumnName(i), rs.getObject(i));
				}
				list.add(rowData);

			}
			data.put("info", info());
			data.put("data", list);
		   
		} catch (SQLException e) {
			return e.getMessage();
		} catch (Exception e) {
			return e.getMessage();
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (conn != null) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				return e.getMessage();
			}
		}
		return data.toJSONString();
	}

	public static void main(String[] args) throws Exception {
		JdbcService jdbc = new JdbcService();

	}
}
